// userPackage/pages/addAddress/addAddress.js
const app = getApp()

// import {
//   cityData3
// } from '.../../../utils/city'
var cityData3 = require('../../../utils/city')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {
      addressDetail: "",
      addressFlag: false,
      addressId: '',
      addressName: "",
      addressTel: "",
      userId: app.globalData.userId
    },
    cityData3: [],
    show: false,
    cascaderValue: [],
    addressStr: '',
    morenFlag: false
  },


  addHandle() {
    var info = this.data.info,
      addressStr = this.data.addressStr,
      morenFlag = this.data.morenFlag,
      url = app.globalData.url + "scUser/addAddress",
      that = this;
    if (info.addressName == '') {
      wx.showToast({
        title: '请输入收件人',
        icon: 'none'
      })
      return
    }
    if (info.addressTel == '') {
      wx.showToast({
        title: '请输入联系电话',
        icon: 'none'
      })
      return
    }
    if (addressStr == '') {
      wx.showToast({
        title: '请选择所在地区',
        icon: 'none'
      })
      return
    }
    if (info.addressDetail == '') {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      })
      return
    }



    console.log(info);
    info.addressFlag = morenFlag ? 1 : 0
    info.addressDetail = addressStr + '--' + info.addressDetail
    console.log(info);
    // return

    wx.showLoading({
      title: '',
    })
    app.wxRequestJson(url, info, res => {
        console.log(res);
        if (res.flag) {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '地址保存成功',
            showCancel: false,
            complete: (res) => {
              if (res.confirm) {
                info.address1 = addressStr
                info.address2 = info.addressDetail.split('--')[1]
                if (info.addressFlag == 1) {
                  wx.setStorageSync('moRenAddress', JSON.stringify(info))
                }
                wx.setStorageSync('address', JSON.stringify(info))
                
                wx.navigateBack()
              }
            }
          })
        }
      },
      err => {
        console.log(err);
      })
  },

  setHandle() {
    console.log(this.data.info);
    var url = app.globalData.url + "scUser/setAddress",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        addressId: that.data.info.addressId,
        userId: app.globalData.userId,
      }, res => {
        console.log(res);
        if (res.flag) {
          wx.hideLoading()
          wx.showToast({
            title: '设置成功',
            icon: 'none'
          })
          info.address1 = that.data.addressStr
          info.address2 = info.addressDetail
          wx.setStorageSync('moRenAddress', JSON.stringify(info))
        }
      },
      err => {
        console.log(err);
      })

  },

  delHandle() {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否要删除该地址',
      complete: (res) => {
        if (res.cancel) {

        }
        if (res.confirm) {

          var url = app.globalData.url + "scUser/deleteAddress";
          wx.showLoading({
            title: '',
          })
          app.wxRequest(url, {
              addressId: that.data.info.addressId,
            }, res => {
              console.log(res);
              if (res.flag) {
                wx.hideLoading()
                wx.showModal({
                  title: '提示',
                  content: '地址删除成功',
                  showCancel: false,
                  complete: (res) => {
                    if (res.confirm) {
                      wx.navigateBack()
                    }
                  }
                })
              }
            },
            err => {
              console.log(err);
            })
        }
      }
    })

  },


  onFlagChange(e) {
    console.log(e);
    var info = this.data.info;
    console.log(info);
    if (info.addressId != '' && e.detail) {
      this.setHandle()
    }
    this.setData({
      morenFlag: e.detail
    })
  },
  onFinish(e) {
    console.log(e);
    var arr = e.detail.selectedOptions;
    this.setData({
      addressStr: arr[0].text + arr[1].text + arr[2].text,
      show: false
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  showHandle() {
    this.setData({
      show: true
    })
  },
  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag,
      val = e.detail.value;
    this.setData({
      [str]: val,
    })
    console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    console.log(options);
    console.log(cityData3);

    if (options.obj) {
      var obj = JSON.parse(options.obj);
      console.log(obj);
      var info = {
        addressDetail: obj.address2,
        addressFlag: obj.addressFlag,
        addressId: obj.addressId,
        addressName: obj.addressName,
        addressTel: obj.addressTel,
        userId: app.globalData.userId
      }
      this.setData({
        addressStr: obj.address1,
        morenFlag: obj.addressFlag == 1,
        info
      })
    }

    this.setData({
      cityData3: cityData3.cityData3,
    })
    console.log(this.data);

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})